define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index',
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'order_id',
                sortName: 'order_id',
                searchFormVisible: false,
                searchFormTemplate: 'customformtpl',
                queryParams: function (params) {
                    var options = table.bootstrapTable('getOptions');
                    var filter = JSON.parse(params.filter);
                    params.filter = JSON.stringify(filter);

                    return params;
                },
                columns: [
                    [
                        {field: 'order_no', title: __('Order_no')},
                        {field: 'box_no', title: __('Box_no')},
                        {field: 'nick_name', title: __('Nick_name')},
                        {field: 'phone', title: __('phone')},
                        {field: 'address', title: __('Address'), align: 'left', formatter: Controller.api.formatter.address_info},
                        {field: 'goods_price', title: __('Goods_price'), operate: 'BETWEEN'},
                        {field: 'pay_price', title: __('Pay_price'), operate: 'BETWEEN'},
                        {field: 'pay_type_text', title: __('Pay_type')},
                        {field: 'status_text', title: __('Status')},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'pay_at', title: __('Pay_at'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.custom
                        },
                    ]
                ]
            });
            
            var submitForm = function (ids, layero) {
                var options = table.bootstrapTable('getOptions');
                console.log(options);
                var columns = [];
                $.each(options.columns[0], function (i, j) {
                    if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                        columns.push(j.field);
                    }
                });
                var search = options.queryParams({});
                $("input[name=search]", layero).val(options.searchText);
                $("input[name=ids]", layero).val(ids);
                $("input[name=filter]", layero).val(search.filter);
                $("input[name=op]", layero).val(search.op);
                $("input[name=columns]", layero).val(columns.join(','));
                $("form", layero).submit();
            };
            $(document).on("click", ".btn-export", function () {
                var ids = Table.api.selectedids(table);
                var page = table.bootstrapTable('getData');
                var all = table.bootstrapTable('getOptions').totalRows;
                console.log(ids, page, all);
                Layer.confirm("请选择导出的选项<form action='" + Fast.api.fixurl("order/export") + "' method='post' target='_blank'><input type='hidden' name='ids' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'><input type='hidden' name='search'><input type='hidden' name='columns'></form>", {
                    title: '导出数据',
                    btn: ["全部(" + all + "条)"],
                    success: function (layero, index) {
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    }
                    , yes: function (index, layero) {
                       submitForm("all", layero);
                        return false;
                    }
                })
            });

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {

                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    if (typeStr == 'one') {
                        $('#table').bootstrapTable('showColumn', 'pay_at');
                        $('#table').bootstrapTable('showColumn', 'updatetime');
                    }

                    if (typeStr == 'two') {

                        filter.status = 0;
                        $('#table').bootstrapTable('hideColumn', 'pay_at');
                        $('#table').bootstrapTable('hideColumn', 'updatetime');
                    }

                    if (typeStr == 'three') {

                        filter.status = 10;
                        $('#table').bootstrapTable('hideColumn', 'updatetime');
                    }

                    if (typeStr == 'four') {

                        filter.status = 20;
                        $('#table').bootstrapTable('hideColumn', 'pay_at');
                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                address_info: function (value, row, index) {
                    if (row.province_name == null) {

                    } else {
                        var html = row.province_name + row.city_name + row.area_name + '<br/>' + row.quarters_name + row.unit_name + row.room_name;
                        return html;
                    }

                },
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var html = '<a href="order/detail/ids/' + row.order_id + '" class="btn-dialog" title="订单详情" data-table-id="table"><b>订单详情</b></a>';

                    return html;
                }
            },
        }
    };
    return Controller;
});